package com.alinesno.cloud.portal.desktop.web.ucenter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alinesno.cloud.base.boot.entity.ManagerApplicationEntity;
import com.alinesno.cloud.base.boot.service.IManagerAccountService;
import com.alinesno.cloud.base.boot.service.IManagerApplicationService;
import com.alinesno.cloud.base.boot.service.IManagerResourceService;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.core.monitor.Server;
import com.alinesno.cloud.common.facade.wrapper.RestWrapper;
import com.alinesno.cloud.common.web.base.controller.BaseController;
import com.alinesno.cloud.common.web.login.aop.DataFilter;
import com.alinesno.cloud.common.web.login.session.CurrentAccountSession;
import com.alinesno.cloud.portal.desktop.web.entity.LinkPathEntity;
import com.alinesno.cloud.portal.desktop.web.service.ILinkPathService;

/**
 * 显示设置
 * @author WeiXiaoJin
 * @since 2019年4月19日 上午6:18:05
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("public/portal/ucenter")
public class UcenterController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(UcenterController.class) ; 

	@Value("${server.servlet.context-path:/}")
	private String serlvetContextPath ;
	
	@Value("${alinesno.devtools.book-type:devbook}")
	private String devBook ; 

	@Autowired
	private IManagerApplicationService managerApplicationService ; 
	
	@Autowired
	private IManagerAccountService managerAccountService ; 
	
	@Autowired
	private IManagerResourceService managerResourceService ; 
	
	@Autowired
	private ILinkPathService LinkPathService ; 

	@DataFilter
	@GetMapping(value = "home")
	public String home(Model model , HttpServletRequest request, RestWrapper restWrapper) throws Exception {
		log.debug("serlvetContextPath:{}" , serlvetContextPath);

		// 取出前几条数据
		List<ManagerApplicationEntity> list = managerApplicationService.findTop(10 , restWrapper) ; 
		model.addAttribute("appList", list) ; 
		
		// 统计应用个数
		int appCount = list.size() ;
		model.addAttribute("appCount", appCount) ; 
		
		// 会员数
		RestWrapper wrapper = new RestWrapper().eq("operatorId", CurrentAccountSession.get(request).getId()) ; 
		long accountCount = managerAccountService.count(wrapper.toSpecification()) ; 
		model.addAttribute("accountCount", accountCount) ;
		
		// 功能数 
		long resourceCount = managerResourceService.count(wrapper.toSpecification()) ; 
		model.addAttribute("resourceCount", resourceCount) ;
	
		// 应用监控
		Server server = new Server();
		server.copyTo();
		model.addAttribute("server", server);
		
		// 开发手册
		
		List<LinkPathEntity> devbooks = LinkPathService.findAll(new RestWrapper().eq("linkType", devBook)) ; 
		model.addAttribute("devbooks" , devbooks) ; 
		
		return "portal/ucenter/home" ; 
	}
	
	@GetMapping(value = "main")
	public String main(Model model , HttpServletRequest request) {
		log.debug("serlvetContextPath:{}" , serlvetContextPath);
		
		return "portal/ucenter/main" ; 
	}
	
}
